package com.bestcem.xm.ticket.dao.mapper;

import com.bestcem.xm.ticket.entity.mysql.TicketConfigDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @author zhangfeng <feng.zhang@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/18 14:30
 */
@Mapper
public interface TicketConfigDaoMapper {

    @Select("select count(distinct survey_id) from `ds_ticket_ticketconfig` where org_id = #{orgId, jdbcType=BINARY} and ticket_rule_id <> ''")
    long countByOrgIdAndTicketRuleId(String orgId);

    @Select("select survey_id,max(ticket_config_id) from `ds_ticket_ticketconfig` where org_id = #{orgId, jdbcType=BINARY} and ticket_rule_id <> '' group by survey_id limit #{offset},#{size}")
    List<TicketConfigDO> listByOrgIdAndTicketRuleId(String orgId, Integer offset, Integer size);

    TicketConfigDO findByTicketConfigId(String ticketConfigId);

    TicketConfigDO findByTitle(String title);

    TicketConfigDO findBySurveyIdAndTitle(String surveyId, String title);

    List<TicketConfigDO> listByOrgId(String orgId);

    List<TicketConfigDO> listByTicketConfigIdList(List<String> ids);

    List<TicketConfigDO> listBySurveyId(String surveyId, Boolean descByCreatedTime);

    void insert(TicketConfigDO ticketConfigDO);

    List<TicketConfigDO> listByOrgIdAndSurveyIdAndState(String orgId, String surveyId, Integer status);

    void updateByTicketConfigId(TicketConfigDO ticketConfigDO);

    TicketConfigDO findByDetailTemplateId(String detailTemplateId);

    TicketConfigDO findByTicketTemplateId(String ticketTemplateId);

    long countBySurveyIdAndTitle(String surveyId, String title);

    int updateDetailTemplateId(String detailTemplateId, String oldDetailTemplateId, String orgId, Date updateTime);

    int updateTicketTemplateId(String ticketTemplateId, String oldTicketTemplateId, String orgId, Date updateTime);

    List<TicketConfigDO> listByOrgIdAndTicketRuleIdNotNull(String orgId, Integer skip, Integer size);

    TicketConfigDO findByOrgIdAndUserId(String orgId, String userId);

    Long countByOrgIdAndTicketRuleIdNotNull(String orgId);

    LinkedHashSet<String> groupSurveyId(String orgId);

    List<TicketConfigDO> listByOrgIdEnable(String orgId, Integer status);

    List<String> listEnabledBySurveyId(String surveyId);


    @Select("select count(*) from `ds_ticket_ticketconfig` " +
            "where org_id = #{orgId, jdbcType=BINARY} " +
            "and ticket_rule_id IS NOT NULL " +
            "and title like concat('%',#{search},'%')")
    long countByOrgIdAndSearch(String orgId, String search);

    List<TicketConfigDO> pageByOrgIdAndSearch(String orgId, String search, int skip, int size);

    List<TicketConfigDO> listByOrgIdAndSearch(String orgId, String search);
}
